package com.xdq.serviceedu.controller;


import com.xdq.commonutils.R;
import com.xdq.servicebase.exceptionhandler.GuliException;
import com.xdq.serviceedu.client.VodClient;
import com.xdq.serviceedu.entity.EduVideo;
import com.xdq.serviceedu.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin // 解决跨域
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) throws GuliException {
        // 查视频 id
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "删除视频失败，熔断器。。。");
            }
        }
        videoService.removeById(id);
        return R.ok();
    }
}

